package com.dm.library.widgets.wheelview.common;


import com.dm.library.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DateUtil {
    private static DateUtil dateUtil;

    private List<String> yearList = new ArrayList<String>(10);

    private List<String> mounthList = new ArrayList<String>(10);

    private List<String> dayList = new ArrayList<String>(10);

    private List<String> hourList = new ArrayList<String>(10);

    private DateUtil() {
    }

    public static DateUtil getInstance() {
        if (null == dateUtil) {
            dateUtil = new DateUtil();
        }
        return dateUtil;
    }

    /**
     * @return 返回 yearList
     */
    public List<String> getYearList() {
        yearList.clear();
        for (int i = 0; i <= 10; i++) {
            yearList.add(2015 + i + "年");
        }
        return yearList;
    }

    /**
     * @return 返回 mounthList
     */
    public List<String> getMounthList() {
        mounthList.clear();
        for (int i = 0; i < 12; i++) {
            StringBuilder sb = new StringBuilder();
            if (i < 9) {
                sb.append("0").append(1 + i).append("月");
            } else {
                sb.append(1 + i).append("月");
            }
            mounthList.add(sb.toString());
        }
        return mounthList;
    }

    /**
     * @return 返回 dayList
     */
    public List<String> getDayList(String year, String mounth) {
        dayList.clear();
        year = year.substring(0, year.length() - 1).trim();
        mounth = mounth.substring(0, mounth.length() - 1).trim();
        int tempYear = Integer.parseInt(year);
        int tempMounth = Integer.parseInt(mounth);
        int days = -1;
        if (tempMounth == 1 || tempMounth == 3 || tempMounth == 5 || tempMounth == 7 || tempMounth == 8 || tempMounth == 10
                || tempMounth == 12) {
            days = 31;
        } else if (tempMounth == 4 || tempMounth == 6 || tempMounth == 9 || tempMounth == 11) {
            days = 30;
        } else if (tempMounth == 2) {
            if (tempYear % 4 == 0) //4年一润
            {
                if (tempYear % 100 == 0 && tempYear % 400 != 0) //百年不润，400百再润
                {
                    days = 28;
                } else {
                    days = 29;
                }
            } else {
                days = 28;
            }
        } else {

        }
        for (int i = 0; i < days; i++) {
            StringBuilder sb = new StringBuilder();
            if (i < 9) {
                sb.append("0").append(1 + i).append("日");
            } else {
                sb.append(1 + i).append("日");
            }
            dayList.add(sb.toString());
        }
        return dayList;
    }

    /**
     * 根据店铺的工作时间来选择预约时间
     *
     * @return 返回 hourList
     */
    public List<String> getHourList(String startTime, String endTime) {
        hourList.clear();
        int start = 0;
        int end = 23;
        //提取上班时间
        if (StringUtils.isEmptyOrNull(startTime)) {
            start = 8;
        } else {
            String[] temp = null;
            if (startTime.contains(":")) {
                temp = startTime.split(":");
            } else if (startTime.contains("：")) {
                temp = startTime.split("：");
            }
            if (null != temp) {
                int hour = Integer.parseInt(temp[0]);
                if (temp.length == 2 && !temp[1].equals("00")) {
                    start = hour + 1;
                } else {
                    start = hour;
                }
            }
        }

        //提取下班时间
        if (StringUtils.isEmptyOrNull(endTime)) {
            end = 19;
        } else {
            String[] temp = null;
            if (startTime.contains(":")) {
                temp = endTime.split(":");
            } else if (startTime.contains("：")) {
                temp = endTime.split("：");
            }

            end = Integer.parseInt(temp[0]);
        }

        //下面的判断可有可无，接口不出问题，这里就不会有问题
        if (start >= end) {
            end = start + 8;
        }
        if (start < 0 || start > 15) {
            start = 0;
        }
        if (end > 23) {
            end = 23;
        }
        for (int i = start; i <= end; i++) {
            hourList.add(i + "  时");
        }
        return hourList;
    }

    public String getDateStr(Date date){
        return  formatString(date,"yyyy-MM-dd HH:mm:ss");
    }
    public String getSimpleDateStr(Date date){
        return  formatString(date,"yyyy-MM-dd");
    }

    private String formatString(Date date,String format){
        SimpleDateFormat sf = new SimpleDateFormat(format);
        String str = "";
        if(date != null){
            str = sf.format(date);
        }
        return  str;
    }
}
